/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.herenit.beijing.poc.empi.converter;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

import org.jdesktop.beansbinding.Converter;
import org.openide.util.Exceptions;

/**
 *
 * @author Obama
 */
public class ByteArrayToImageConverter extends Converter {

    @Override
    public Object convertForward(Object s) {
        BufferedImage bi = null;
        if (s instanceof byte[]) {
            byte[] t = (byte[]) s;
            try {
                bi = ImageIO.read(new ByteArrayInputStream(t));
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            } finally {
                return bi;
            }

        }
        return bi;
    }

    @Override
    public Object convertReverse(Object t) {
        Image bi = null;
        if (t instanceof Image) {
            bi = (Image) t;
        }
        return bi;
    }
}
